package com.ruoyi.admin.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.admin.service.IDepartmentService;
import com.ruoyi.admin.vo.HospitalVO;
import com.ruoyi.admin.vo.UserRoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.admin.domain.Hospital;
import com.ruoyi.admin.service.IHospitalService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 医院信息Controller
 * 
 * @author ruoyi
 * @date 2022-10-23
 */
@RestController
@RequestMapping("/hospital")
public class HospitalController extends BaseController
{
    @Autowired
    private IHospitalService hospitalService;

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询医院信息列表
     */
    @RequiresPermissions("admin:hospital:list")
    @GetMapping("/list")
    public TableDataInfo list(Hospital hospital)
    {
        startPage();
        List<HospitalVO> list = hospitalService.selectAllHospital(hospital);
        return getDataTable(list);
    }

    /**
     * 导出医院信息列表
     */
    @RequiresPermissions("admin:hospital:export")
    @Log(title = "医院信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Hospital hospital)
    {
        List<Hospital> list = hospitalService.selectHospitalList(hospital);
        ExcelUtil<Hospital> util = new ExcelUtil<Hospital>(Hospital.class);
        util.exportExcel(response, list, "医院信息数据");
    }

    /**
     * 获取医院信息详细信息
     */
    @RequiresPermissions("admin:hospital:query")
    @GetMapping(value ={"/", "/{hospitalId}"})
    public AjaxResult getInfo(@PathVariable(value ="hospitalId",required = false) Long hospitalId)
    {
        List<UserRoleVO> userRoleVOS = hospitalService.selectAllDuijie();
        List<UserRoleVO> userRoleVOS1 = hospitalService.selectAllGuDong();
        List<UserRoleVO> userRoleVOS2 = hospitalService.selectAllYiJi();
        List<UserRoleVO> userRoleVOS3 = hospitalService.selectAllErJi();

        AjaxResult ajax = AjaxResult.success();
        ajax.put("xiugai",hospitalService.selectHospitalByHospitalId(hospitalId));
        ajax.put("duijie",userRoleVOS);
        ajax.put("gudong",userRoleVOS1);
        ajax.put("yiji",userRoleVOS2);
        ajax.put("erji",userRoleVOS3);

        ajax.put("dept",departmentService.selectHospitalDept(hospitalId));
        ajax.put("hospital",hospitalService.selectHospitalByHospitalId(hospitalId));
        return ajax;
    }

    /**
     * 新增医院信息
     */
    @RequiresPermissions("admin:hospital:add")
    @Log(title = "医院信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Hospital hospital)
    {
        return toAjax(hospitalService.insertHospital(hospital));
    }

    /**
     * 修改医院信息
     */
    @RequiresPermissions("admin:hospital:edit")
    @Log(title = "医院信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Hospital hospital)
    {
        return toAjax(hospitalService.updateHospital(hospital));
    }

    /**
     * 删除医院信息
     */
    @RequiresPermissions("admin:hospital:remove")
    @Log(title = "医院信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{hospitalIds}")
    public AjaxResult remove(@PathVariable Long[] hospitalIds)
    {
        return toAjax(hospitalService.deleteHospitalByHospitalIds(hospitalIds));
    }
}
